package kgame5.kgu.ai {
import kgame5.kgu.move.MapManager;
import kgame5.k3d.core.math.Vector3D;
import kgame5.k3d.core.obj3d.hardvo.HardVO;
import kgame5.kmath.MathDG;

/**
 * @author kk
 * 根据制定的路径寻径。ArrayList<int[]>
 * [1,2]
 * [2,3]
 * [3,4]
 * .
 * .
 * .
 */
public class PathFollower {
	//--
	private var hardVO:HardVO;//要操纵的hardVO
	
	private var p:Vector3D;//快捷引用
	private var CellSize:int;
	
	//--
	private var speed:int;
	
	private var a2Route:Array;//ArrayList<int[]> 用ArrayList是因为寻径的时候就是这样的
	
	private var step:int=-1;//路径的块数
	
	private var aimX:int;
	private var aimZ:int;
	
	private var running:Boolean=false;
	
	public function PathFollower(hardVO:HardVO ,CellSize:int){
		this.hardVO=hardVO;
		
		p= hardVO.v3d_p;
		
		this.CellSize=CellSize;
	}

	//
	public function init(a2Route:Array,speed:int):void{
		
		this.a2Route=a2Route;
		this.speed=speed;
		step=-1;//因为nextStep里会先step++的
		
		//!!这个很重要，防止使用PosiM来定位，因为这里是自己直接定位
		hardVO.v3d_v.fill(0,0,0);
		
		running=true;
		
		next();
	}
	
	//下一个格子
	private function next():void{
		if(step==a2Route.size()-1){
			running=false;
			return;
		}
		
		step++;
		var point:Array=a2Route[step];
		aimX= MapManager.calcX(point[0],CellSize) ;//(a2Route[step][0],CellSize);
		aimZ= MapManager.calcZ(point[1],CellSize) ;//PosiM.calcZ(a2Route[step][1],CellSize);
		hardVO.v3d_r.y=MapManager.calcDir(p.x,p.z,aimX,aimZ);
	}
	
	//
	public function update():Boolean{
		if(running==false){
			return true;
		}
		
			p.x=MathDG.W(p.x,aimX,speed);
			p.z=MathDG.W(p.z,aimZ,speed);
			
			if(p.x==aimX&&p.z==aimZ){
				next();
			}
			
			return false;
		
	}
}//end of class

}